package com.example.weather.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.weather.Adapter.Cityfragmentadapter;
import com.example.weather.DB.Dbmanager;
import com.example.weather.Fragment.CityweatherFragment;
import com.example.weather.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView main_iv_add,main_iv_more;
    private LinearLayout main_layout_point;
    private ViewPager main_vp;
    private RelativeLayout main_out_layout;
//viewpager的数据源
    List<Fragment> fragmentList;
    //需要显示的城市的集合
    List<String> citylist;
    //viewpager的页面指示器显示集合
    List<ImageView> imglist;
    private Cityfragmentadapter adapter;
    private SharedPreferences pref;
    private int bg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        main_iv_add.setOnClickListener(this);
        main_iv_more.setOnClickListener(this);
        fragmentList=new ArrayList<>();
        exchangebg();
        citylist= Dbmanager.allcityname();//获取数据库包含的城市列表
        imglist=new ArrayList<>();
        //获取添加界面的城市值
        Intent intent=getIntent();
        String city = intent.getStringExtra("city");
        if (!citylist.contains(city)&&!TextUtils.isEmpty(city)) {
            citylist.add(city);
        }


        if (citylist.size()==0){
            citylist.add("北京");
            citylist.add("上海");
        }
        //初始化viewpager文档页面
        initpager();
        adapter = new Cityfragmentadapter(getSupportFragmentManager(), fragmentList);
        main_vp.setAdapter(adapter);
        //小圆点指示器
        initpoint();
        //设置最后一个城市
        main_vp.setCurrentItem(fragmentList.size()-1);
        //viewpager页面监听
        setviewpagerlistener();
    }

    private void setviewpagerlistener() {
        main_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            //选择的页面位置
            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < imglist.size(); i++) {
                    imglist.get(i).setImageResource(R.mipmap.a1);
                }
                imglist.get(position).setImageResource(R.mipmap.a2);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initpoint() {
        //创建小圆点
        for (int i = 0; i < fragmentList.size(); i++) {
            ImageView piv = new ImageView(this);
            piv.setImageResource(R.mipmap.a1);
            piv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
            LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams) piv.getLayoutParams();
            lp.setMargins(0,0,20,0);
            imglist.add(piv);
            main_layout_point.addView(piv);
        }
        imglist.get(imglist.size()-1).setImageResource(R.mipmap.a2);
    }

    private void initpager() {
        //创建fragment对象，添加到viewpager数据源中
        for (int i = 0; i <citylist.size(); i++) {
            CityweatherFragment cityweatherFragment = new CityweatherFragment();
            Bundle bundle = new Bundle();
            bundle.putString("city",citylist.get(i));
            cityweatherFragment.setArguments(bundle);
            fragmentList.add(cityweatherFragment);
        }
    }

    private void init(){
        main_iv_add=findViewById(R.id.main_iv_add);
        main_iv_more=findViewById(R.id.main_iv_more);
        main_layout_point=findViewById(R.id.main_layout_point);
        main_vp=findViewById(R.id.main_vp);
        main_out_layout=findViewById(R.id.main_out_layout);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_iv_add:
                startActivity(new Intent(MainActivity.this,CityManagerActivity.class));
                break;
            case R.id.main_iv_more:
                startActivity(new Intent(MainActivity.this,MoreActivity.class));
                break;
        }
    }

    //页面进入重新获取焦点前的方法，完成viewpager页数的跟新
    @Override
    protected void onRestart() {
        super.onRestart();
        //获取数据库中还剩下的城市
        List<String> list = Dbmanager.allcityname();
        if (list.size()==0) {
            list.add("北京");
        }
        //重新加载前，清空原来的数据源
        citylist.clear();
        citylist.addAll(list);
        //剩余城市创建对应的fragment页面
        fragmentList.clear();
        initpager();
        adapter.notifyDataSetChanged();
        //页面指示器的数量变化，重新设置
        imglist.clear();
        main_layout_point.removeAllViews();//布局中所有元素移除
        initpoint();
        main_vp.setCurrentItem(fragmentList.size()-1);
    }
    //换壁纸函数
    public void exchangebg(){
        pref = getSharedPreferences("bg_pref", MODE_PRIVATE);
        bg = pref.getInt("bg", 2);
        switch (bg) {
            case 0:
                main_out_layout.setBackgroundResource(R.mipmap.bg);
                break;
            case 1:
                main_out_layout.setBackgroundResource(R.mipmap.bg2);
                break;
            case 2:
                main_out_layout.setBackgroundResource(R.mipmap.bg3);
                break;
        }
    }
}
